package com.xxcxcxcx.jobsystem.common.Job;

import com.xxcxcxcx.jobsystem.common.JobsClass;
import com.xxcxcxcx.jobsystem.common.capability.JobCapabilityProvider;
import net.minecraft.ChatFormatting;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.player.Player;

public abstract class JobBase {
    public static void UpExperience(Player player, double num,String Job){
        if(num==0)return;
        if(!(player instanceof ServerPlayer))return;
        JobCapabilityProvider.getPlayerData(player).ifPresent(playerSaveData -> {
            int level = playerSaveData.getLevel(Job);
            double experience = playerSaveData.getExperience(Job);
            double coefficient = playerSaveData.getCoefficient(Job);
            double r1=player.getRandom().nextDouble();
            double r2=player.getRandom().nextDouble();
            double r3=player.getRandom().nextDouble();
            double num2 = (num * coefficient * ((Math.sqrt(r1)/(1+r1))+0.5));
            if(level>=4)num2 *= (r2*0.25+0.75);
            if(level>=7)num2 *= (r3*r3*0.5+0.5);
            if(num2<0)num2=0;
            if(playerSaveData.isReadGainExperience())player.sendMessage((new TranslatableComponent("jobsystem.commands.gainexperience",new TranslatableComponent("jobsystem.job."+Job).withStyle(new ChatFormatting[]{ChatFormatting.GOLD}),new TranslatableComponent(Double.toString(num2)).withStyle(new ChatFormatting[]{ChatFormatting.BLUE}))), player.getUUID());
            if(level==10)playerSaveData.addExperience(Job,num2);
            else if(experience+num2>JobsClass.xp[level]){
                playerSaveData.setExperience(Job,experience+num2-JobsClass.xp[level]);
                player.sendMessage((new TranslatableComponent("jobsystem.commands.levelup",new TranslatableComponent("jobsystem.job."+Job).withStyle(new ChatFormatting[]{ChatFormatting.GOLD}),new TranslatableComponent(Integer.toString(level+1)).withStyle(new ChatFormatting[]{ChatFormatting.RED}))), player.getUUID());
                playerSaveData.addLevel(Job,1);
            }
            else {
                playerSaveData.addExperience(Job,num2);
            }
        });
    }
}
